package com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.CityInfo;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiIndoorResult;
import com.baidu.mapapi.search.poi.PoiNearbySearchOption;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.baidu.mapapi.search.poi.PoiSortType;
import com.diudiutechnology.www.yadiudiu.util.utils.Constant;
import com.diudiutechnology.www.yadiudiu.util.utils.MyProgressDialog;
import com.diudiutechnology.www.yadiudiu.util.utils.ToastUtils;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyApplication;
import com.diudiutechnology.www.yadiudiu.R;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyAdapter.AddressSearchActivityAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/8/24.\
 * 搜索地址
 */

public class AddressSearchActivity extends Activity implements View.OnClickListener,OnGetPoiSearchResultListener {
    @Bind(R.id.retur)
    LinearLayout retur;
    @Bind(R.id.task_search)
    EditText task_search;
    @Bind(R.id.determine)
    TextView determine;

    Context context;

    @Bind(R.id.listview)
    ListView listview;
    List<PoiInfo> allPoi=new ArrayList<>();
    AddressSearchActivityAdapter adapter;
    private MyProgressDialog mDialog;
    private PoiSearch mPoiSearch = null;
    String pos="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_search);
        ButterKnife.bind(this);
        context=this;
        initView();
    }

    private void initView() {
        retur.setOnClickListener(this);
        determine.setOnClickListener(this);
if (getIntent()!=null){
    pos=getIntent().getStringExtra("pos");
}

        adapter=new AddressSearchActivityAdapter(context);
        listview.setAdapter(adapter);
        // 初始化搜索模块，注册搜索事件监听
        mPoiSearch = PoiSearch.newInstance();
        mPoiSearch.setOnGetPoiSearchResultListener(this);
        searchButtonProcess("小区");
        listview.setOnItemClickListener(clickListener);
    }
AdapterView.OnItemClickListener clickListener=new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        PoiInfo poiInfo = allPoi.get(i);
        LatLng location = poiInfo.location;
        Intent intent=new Intent();
        intent.putExtra("latitude",location.latitude+"");
        intent.putExtra("longitude",location.longitude+"");
        intent.putExtra("name",poiInfo.name);
        intent.putExtra("address",poiInfo.address);
        if (pos.equals("0")){
            setResult(Constant.SELECT,intent);
        }else   if (pos.equals("1")){
            setResult(Constant.ADDADDRESSENTAL,intent);
        }
        finish();
    }
};

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.retur:
                finish();
                break;
            case R.id.determine:
                if (TextUtils.isEmpty(task_search.getText().toString().trim())){//地址
                    ToastUtils.show(context, "请输入搜索地址");
                    return;
                }
                searchButtonProcess(task_search.getText().toString().trim());
                break;

        }
    }
      /** 响应城市内搜索按钮点击事件
     *
      * @param
    */
    public void searchButtonProcess( String keystr) {


        mDialog =new MyProgressDialog(context);
        if (pos.equals("1")){
            String citystr = MyApplication.getInstance().getCity();
            mPoiSearch.searchInCity((new PoiCitySearchOption())
                    .city(citystr).keyword(keystr).pageNum(0));
        }else {
            LatLng center = new LatLng(MyApplication.getInstance().getLatitude(), MyApplication.getInstance().getLongitude());
            PoiNearbySearchOption nearbySearchOption = new PoiNearbySearchOption().keyword(keystr)
                    .sortType(PoiSortType.distance_from_near_to_far).location(center)
                    .radius(100).pageNum(0);
            mPoiSearch.searchNearby(nearbySearchOption);
        }

    }
    @Override
    protected void onDestroy() {
        mPoiSearch.destroy();

        super.onDestroy();
    }

    @Override
    public void onGetPoiResult(PoiResult result) {
        if (mDialog != null) {
            mDialog.dismiss();
        }
        if (result == null || result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
            Toast.makeText(AddressSearchActivity.this, "未找到结果", Toast.LENGTH_LONG)
                    .show();
            return;
        }
        if (result.error == SearchResult.ERRORNO.NO_ERROR) {
            allPoi.clear();
            // 将周边信息增加表
            if (result.getAllPoi() != null) {
                allPoi.addAll(result.getAllPoi());
            }

            adapter.addItem(allPoi);
            return;
        }
        if (result.error == SearchResult.ERRORNO.AMBIGUOUS_KEYWORD) {

            // 当输入关键字在本市没有找到，但在其他城市找到时，返回包含该关键字信息的城市列表
            String strInfo = "在";
            for (CityInfo cityInfo : result.getSuggestCityList()) {
                strInfo += cityInfo.city;
                strInfo += ",";
            }
            strInfo += "找到结果";
            Toast.makeText(AddressSearchActivity.this, strInfo, Toast.LENGTH_LONG)
                    .show();
        }
    }

    @Override
    public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {
        if (poiDetailResult.error != SearchResult.ERRORNO.NO_ERROR) {
            ToastUtils.show(AddressSearchActivity.this, "抱歉，未找到结果");
        } else {
            ToastUtils.show(AddressSearchActivity.this, poiDetailResult.getName() + ": " + poiDetailResult.getAddress());
        }
    }

    @Override
    public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {

    }
}
